import BasePageCtrl, { AnimType } from "./BasePageCtrl";
import n from "./AudioManager"
import l from "./AbTestMgr"
import s from "./PeripheralSystemDataMgr"
import c from "./EventMgr"
import u from "./MatchGameEventType"
import p from "./frameworkManager"
import d from "./AdManager"
import h from "./SdkHelper"
import f from "./EngineUtil"
import g from "./MatchGameData"
import y from "./MatchGameAudioMgr"
import m from "./BasePageCtrl"
import _ from "./MatchGameSystemMgr"
import v, { VideoBuriedPoint } from "./PeripheralSystemMgr"
import b from "./UiManage"
import MatchGameSuccessJJPage from "./MatchGameSuccessJJPage";
import UiManager from "./UiManage";
import NativeUtils from "./NativeUtils";
const {
    ccclass,
    property
} = cc._decorator;

@ccclass
export default class MatchGameSuccessJJPageCtrl extends BasePageCtrl {
    ui = null;
    canClick = false;
    step = 0;
    _animType;
    _touchControl = false;
    _hasPeneLock = true;
    _hasBlack = true;
    _hasTouchLock = false;
    _hasBlackTouch = false;

    onLoad() {
        this.onUILoad();
        this._animType = AnimType.SCALE;
        this._touchControl = false;
        this._hasPeneLock = true;
        this._hasBlack = true;
        this._hasTouchLock = false;
        this._hasBlackTouch = false;
        this.addBtnListeners();
        super.onLoad();
    };

    start() { };

    onUILoad() {
        this.ui = this.node.addComponent(MatchGameSuccessJJPage);
    };

    _init(data?) {
        n.getInstance().pauseMusic("bgm", true);
        this.canClick = false;
        this.node.setContentSize(cc.winSize);
        var startFlyNode = data.strtFlyNode;
        var step = data.step;
        this.step = step;
        this.ui.goldCoinFlySpine.active = false;
        this.ui.hb_sp.active = false;
        this.ui.jj_sp.active = false;
        var isReviewBan = s.isHitOppoReviewBan();
        this.ui.lab_jj_count.active = !isReviewBan;
        this.playFlyAnimation(startFlyNode);
        g.getFlyBoxCash();
        _.matchGameSuccess(this.step, (e) => {
            var goldBalance = "0";
            if (e && e.gold) {
                goldBalance = f.getCashBalance(e.gold);
            }
            h.reportData("matchgame_finish_game_success", {
                level: g.gameLevel,
                game_time: g.getGameTime()
            });
            this.ui.lab_jj_count.getComponent(cc.Label).string = goldBalance + "y";
            p.log("gold----", goldBalance);
            p.log("通关成功");
        });
        s.setRollingNoticeParent(this.ui.noticeParent);
        c.trigger(u.HIDE_MATCHGAME_NODE);
    };

    playFlyAnimation(startFlyNode) {
        var isReviewBan = s.isHitOppoReviewBan();
        this.ui.goldCoinFlySpine.active = true;
        this.ui.goldCoinFlySpine.position = cc.v3(f.getNodeAToNodeBPoint(startFlyNode, this.node));
        var spineSkeleton = this.ui.goldCoinFlySpine.getComponent(sp.Skeleton);
        y.playSuccessPageShowAudio();
        spineSkeleton.setAnimation(0, "idle_2", false);
        spineSkeleton.setAnimation(1, "idle_3", false);

        var hbSpPositionX = isReviewBan ? 0 : this.ui.hb_sp.x;
        cc.tween(spineSkeleton.node)
            .delay(0.5)
            .to(0.6, { x: hbSpPositionX, y: this.ui.hb_sp.y }, { easing: "backOut" })
            .call(() => {
                spineSkeleton.node.active = false;
                this.ui.hb_sp.active = true;
                this.ui.content.active = true;
                this.ui.sp_double_btn.active = false;
                this.ui.hb_sp.x = isReviewBan ? 0 : this.ui.hb_sp.x;
                this.ui.hb_sp.getComponent(sp.Skeleton).setAnimation(0, "idle_1", false);
                this.ui.hb_sp.getComponent(sp.Skeleton).setAnimation(1, "idle_2", true);
                this.canClick = true;
                var isBtljFlag = l.isBtljFlag();
                //this.ui.btn_normal.getComponent(cc.Label).string = isBtljFlag ? "红包只领取" + (this.step / 100).toFixed(2) + "元" : "只领取" + (this.step / 100).toFixed(2) + "元红包";
            })
            .start();

        this.ui.jj_sp.x = -1000;
        this.ui.jj_sp.active = !isReviewBan;
        cc.tween(this.ui.jj_sp)
            .delay(0.5)
            .to(0.6, { x: -this.ui.hb_sp.x, y: this.ui.hb_sp.y }, { easing: "backOut" })
            .call(() => {
                this.ui.jj_sp.getComponent(sp.Skeleton).setAnimation(0, "idle_1", false);
                this.ui.jj_sp.getComponent(sp.Skeleton).setAnimation(1, "idle_2", true);
            })
            .start();
    };

    addBtnListeners() {
        UiManager.addButtonListen(this.ui.btn_double, this.doubleCallback, this);
        UiManager.addButtonListen(this.ui.btn_normal, this.normalCallback, this);
    };

    doubleCallback() {
        if (this.canClick) {
            h.reportData("matchgame_click_double_reward", {
                level: g.gameLevel,
                game_time: g.getGameTime()
            });
            s.reportEncourageVideo();
            this.ui.sp_double_btn.getComponent(sp.Skeleton).setAnimation(0, "idle_2", false);
            this.ui.sp_double_btn.getComponent(sp.Skeleton).setAnimation(1, "idle_1", true);
            NativeUtils.showVideoAd(7, () => {
                var isArriveNewCity = s.isArriveNewCity();
                _.successDouble(this.step, isArriveNewCity, () => {
                    this.hide();
                    c.trigger(u.END_MATCH_GAME, {
                        is_tg: 1
                    });
                    h.reportData("matchgame_get_double_reward", {
                        level: g.gameLevel,
                        game_time: g.getGameTime()
                    });
                });
            });
        }
    };

    normalCallback() {
        if (this.canClick) {
            h.reportData("matchgame_click_normal_reward", {
                level: g.gameLevel,
                game_time: g.getGameTime()
            });
            var forceVideo = g.force_video;
            var isBtlj = l.isBtljFlag();
            s.isHitOppoReviewBan() && s.setRewardGold(0);
            if (forceVideo) {
                s.reportEncourageVideo();
                h.showForceToast("看完广告就能获得大量红包");
                d.getInstance().playNormalVideoAd(() => {
                    s.reportVideoBuriedPoint(VideoBuriedPoint.FORCE_ENCOURAGE);
                    var isArriveNewCity = s.isArriveNewCity();
                    _.successDouble(this.step, isArriveNewCity, () => {
                        this.hide();
                        c.trigger(u.END_MATCH_GAME, {
                            is_tg: 1
                        });
                    });
                }, () => {
                    if (!isBtlj) {
                        s.setRewardGold(0);
                    }
                    this.hide();
                    c.trigger(u.END_MATCH_GAME, {
                        is_tg: 1
                    });
                });
            } else {
                if (!isBtlj) {
                    s.setRewardGold(0);
                }
                this.hide();
                c.trigger(u.END_MATCH_GAME, {
                    is_tg: 1
                });
            }
        }
    };

    clickClose() {
        this.hide();
    };

    _onHide() {
        m.prototype._onHide.call(this);
    };

    public _onShow(): void {
        super._onShow();
        NativeUtils.showScreenAds(5,()=>{},true)
    }
    
    public hide(): void {
        super.hide();
        NativeUtils.showScreenAds(5,()=>{},false)
    }
    
}

// MatchGameSuccessJJPageCtrl.prefabUrl = "MatchGameSuccessJJPage";
// MatchGameSuccessJJPageCtrl.className = "MatchGameSuccessJJPageCtrl";